//
//  ATMyFavoriteViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATMyFavoriteViewController.h"
#import "FavorGoodsViewController.h"
#import "FavorArticleViewController.h"
#import "ImageLabelView.h"
#import "TTNavView.h"


@interface ATMyFavoriteViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) FavorGoodsViewController *goodsVC;
@property (nonatomic, strong) FavorArticleViewController *articleVC;
@property (nonatomic, strong) ImageLabelView *articleView;
@property (nonatomic, strong) ImageLabelView *goodsView;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) TTNavView *navView;

@end

@implementation ATMyFavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addOwnNav];
    [self addChildVCTitle];
    [self addChildVC];
    [self changeChildVC];
}

// 自定义导航栏
- (void)addOwnNav
{
    self.navView = [[TTNavView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    self.navigationItem.title = @"收藏";
    [self.view addSubview:self.navView];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth / 2 - 1, 75, 2, 25)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:line];
    
}

// 添加子控制器标题
- (void)addChildVCTitle
{
    self.goodsView = [[ImageLabelView alloc] initWithFrame:CGRectMake(0, 70, kWidth / 2, 40)];
    _goodsView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji-2"];
    _goodsView.label.text = @"萌货";
    _goodsView.tag = 101;
    _goodsView.label.font = [UIFont systemFontOfSize:14];
    _goodsView.label.textColor = [UIColor redColor];
    [self.view addSubview:_goodsView];
    
    self.articleView = [[ImageLabelView alloc] initWithFrame:CGRectMake(_goodsView.right, _goodsView.top, kWidth / 2, 40)];
    _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang"];
    _articleView.label.text = @"萌文";
    _articleView.tag = 102;
    _articleView.label.font = [UIFont systemFontOfSize:14];
    _articleView.label.textColor = [UIColor lightGrayColor];
    [self.view addSubview:_articleView];
    
}

// 添加子控制器
- (void)addChildVC
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 104, kWidth, kHeight - 104)];
    self.scrollView.contentSize = CGSizeMake(2 * kWidth, self.scrollView.height);
    self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h@2x"]];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = YES;
    self.scrollView.alwaysBounceHorizontal = YES;
    self.scrollView.alwaysBounceVertical = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    self.goodsVC = [[FavorGoodsViewController alloc] init];
    _goodsVC.view.frame = CGRectMake(0, 0, kWidth, self.scrollView.height);
    [self.scrollView addSubview:_goodsVC.view];
    [self addChildViewController:_goodsVC];
    
    self.articleVC = [[FavorArticleViewController alloc] init];
    _articleVC.view.frame = CGRectMake(kWidth, 0, kWidth, self.scrollView.height);
    [self.scrollView addSubview:_articleVC.view];
    [self addChildViewController:_articleVC];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scrollView.contentOffset.x == 0) {
        _goodsView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji-2"];
        _goodsView.label.textColor = [UIColor redColor];
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang"];
        _articleView.label.textColor = [UIColor lightGrayColor];
        
    } else {
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang-2"];
        _articleView.label.textColor = [UIColor redColor];
        _goodsView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji"];
        _goodsView.label.textColor = [UIColor lightGrayColor];
        
    }

}

// 切换子控制器
- (void)changeChildVC
{
    UITapGestureRecognizer *goodsTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goodsTap)];
    [_goodsView addGestureRecognizer:goodsTap];
    goodsTap.delegate = self;
    goodsTap.cancelsTouchesInView = NO;
    
    UITapGestureRecognizer *articleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(articleTap)];
    [_articleView addGestureRecognizer:articleTap];
    articleTap.delegate = self;
    articleTap.cancelsTouchesInView = NO;
}

// 点击文章标题切换视图
- (void)goodsTap
{
    [UIView animateWithDuration:0.5 animations:^{
        _goodsView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji-2"];
        _goodsView.label.textColor = [UIColor redColor];
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang"];
        _articleView.label.textColor = [UIColor lightGrayColor];
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }];
}

// 点击专辑标题切换视图
- (void)articleTap
{
    [UIView animateWithDuration:0.5 animations:^{
        _goodsView.imageV.image = [UIImage imageNamed:@"iconfont-zhaunji"];
        _goodsView.label.textColor = [UIColor lightGrayColor];
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang-2"];
        _articleView.label.textColor = [UIColor redColor];
        [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
        self.rightButton.hidden = YES;
    }];
}

// 手势代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
